package com.design.pattern.combinationAndVisitor;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


/**
 * 检查权限是否被关联的操作
 * @author zhonghuashishan
 *
 */
@Component
@Scope("prototype") 
public class RelatedCheckPriorityOperation implements PriorityOperation<Boolean> {  

	/**
	 * 关联检查结果
	 */
	private Boolean relateCheckResult = false;

	
	/**
	 * 访问权限树节点
	 */
	@Override
	public Boolean doExecute(Priority node) throws Exception {
//		List<PriorityDO> priorityDOs = priorityDAO
//				.listChildPriorities(node.getId());
//
//		if(priorityDOs != null && priorityDOs.size() > 0) {
//			for(PriorityDO priorityDO : priorityDOs) {
//				Priority priorityNode = priorityDO.clone(Priority.class);
//				priorityNode.execute(this);
//			}
//		}
		System.out.println("根据id查询数据库 然后向上或向下组装树形结构");

		System.out.println("节点调用 execute(this)  每一个节点都执行当前的操作 添加、删除、修改");

		
		if(relateCheck(node)) {
			this.relateCheckResult = true;
		}
		
		return this.relateCheckResult;
	}
	
	/**
	 * 检查权限是否被任何一个角色或者是账号关联了 
	 * @param node 权限树节点
	 * @return 是否被任何一个角色或者是账号关联了，如果有关联则为true；如果没有关联则为false
	 */
	private Boolean relateCheck(Priority node) throws Exception {
//		Long roleRelatedCount = rolePriorityRelationshipDAO
//				.countByPriorityId(node.getId());
//		if(roleRelatedCount != null && roleRelatedCount > 0) {
//			return true;
//		}
//
//		Long accountRelatedCount = accountPriorityRelationshipDAO
//				.countByPriorityId(node.getId());
//		if(accountRelatedCount != null && accountRelatedCount > 0) {
//			return true;
//		}
		
		return false;
	}

	public Boolean getRelateCheckResult() {
		return relateCheckResult;
	}
	
}
